﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitFamilyDataBase;
using YJKExcelFormTool.Creator;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Kits;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Forms
{
	public partial class MaterialTableCommonForm : System.Windows.Forms.Form
	{
		public TableInfos TheTableInfos
		{
			get
			{
				return this.m_TableInfos;
			}
		}

		public MaterialTableCommonForm(ExternalCommandData cmdData, TableInfos tableInfos, CheckedListBox checkedListBox, List<CombinationOfAttributes> categoryInfos, MaterialProgramInfos._Tag_TemplateType templateType, string templatePath, SubjectType subjectType)
		{
			this.m_Revit = cmdData;
			this.m_TableInfos = tableInfos;
			this.m_CategorySelectedListBox = checkedListBox;
			this.m_AllCombinationOfAttributesList = categoryInfos;
			this.m_TemplateType = templateType;
			if (File.Exists(templatePath))
			{
				this.m_TemplatePath = templatePath;
			}
			else
			{
				this.m_TemplatePath = this.GetDefaultTemplatePath();
			}
			this.m_SubjectType = subjectType;
			this.m_ProfessionType = this.GetProfessionTypeName();
			this.InitializeComponent();
			this.InitData();
			this.m_Previewer = new Previewer(this, this.grpBoxHeader, 5, 15, 540, 244, 1u);
			base.MouseWheel += this.m_Previewer.MouseWheel;
		}

		private string GetDefaultTemplatePath()
		{
			string result;
			if (this.m_SubjectType == SubjectType.Electric)
			{
				result = AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\";
			}
			else if (this.m_SubjectType == SubjectType.HVAC)
			{
				result = AssistFunc.GetAppRootPath() + "\\Data\\HVACData\\";
			}
			else
			{
				result = AssistFunc.GetAppRootPath() + "\\Data\\PlumbingData\\";
			}
			return result;
		}

		private string GetProfessionTypeName()
		{
			string result;
			if (this.m_SubjectType == SubjectType.Electric)
			{
				result = "电气";
			}
			else if (this.m_SubjectType == SubjectType.HVAC)
			{
				result = "暖通";
			}
			else
			{
				result = "给排水";
			}
			return result;
		}

		protected override void DefWndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg == 1125)
			{
				if (m.WParam.ToInt32() == 1)
				{
					if (this.m_TableInfos.IsNull())
					{
						return;
					}
					TableDrawingKitForPicture tableDrawingKitForPicture = new TableDrawingKitForPicture(this.m_TableInfos.Head, this.m_TableInfos.FontHeight());
					try
					{
						tableDrawingKitForPicture.Drawing();
					}
					catch (Exception)
					{
					}
					this.m_Previewer.Curves = tableDrawingKitForPicture.Curves;
					this.m_Previewer.TextNotes = tableDrawingKitForPicture.Texts;
					this.m_Previewer.FontName = this.m_TableInfos.FontName();
					this.m_Previewer.FontHeight = Common.MMToFeet(this.m_TableInfos.FontHeight());
					return;
				}
			}
			else
			{
				base.DefWndProc(ref m);
			}
		}

		private void btnSelectTemplate_Click(object sender, EventArgs e)
		{
			if (this.m_TemplateType == MaterialProgramInfos._Tag_TemplateType.TT_FAMILY)
			{
				this.SelectFamilyHeader();
			}
			else if (this.m_TemplateType == MaterialProgramInfos._Tag_TemplateType.TT_EXCEL)
			{
				this.SelectExcelHead();
			}
			this.SetColMappint();
		}

		private void SelectFamilyHeader()
		{
			List<SymbolTypeHeadInfos> headSymbolTypeList = TableCreator.GetHeadSymbolTypeList(this.m_Revit, "材料表");
			if (headSymbolTypeList.Count == 0)
			{
				YJKMessageBox.Show("当前族库中缺少" + this.m_ProfessionType + "表头类型，请添加表头类型后，重新选择");
				return;
			}
			using (SelectTableHeaderForm selectTableHeaderForm = new SelectTableHeaderForm(this.m_Revit, headSymbolTypeList, this.m_TableInfos.FontName(), this.m_TableInfos.FontHeight(), this.m_TableInfos.FontWidthScale()))
			{
				if (selectTableHeaderForm.ShowDialog(this) != DialogResult.OK)
				{
					return;
				}
				new MaterialTableCreater(this.m_Revit);
				this.m_TableInfos.Head = selectTableHeaderForm.SelectTableHead;
			}
			this.Refresh();
		}

		private void SelectExcelHead()
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			string text = this.m_TemplatePath;
			int num = text.LastIndexOf("\\");
			if (num != -1)
			{
				text = text.Substring(0, num) + "\\";
			}
			openFileDialog.InitialDirectory = text;
			openFileDialog.Filter = "Excel文件|*.xls|Excel文件|*.xlsx";
			openFileDialog.RestoreDirectory = true;
			openFileDialog.FilterIndex = 1;
			if (openFileDialog.ShowDialog(this) != DialogResult.OK)
			{
				return;
			}
			using (ImportExcelTableHeadForm importExcelTableHeadForm = new ImportExcelTableHeadForm(openFileDialog.FileName, this.m_TableInfos.FontName(), this.m_TableInfos.FontHeight(), this.m_TableInfos.FontWidthScale()))
			{
				if (importExcelTableHeadForm.ShowDialog(this) != DialogResult.OK)
				{
					return;
				}
				new MaterialTableCreater(this.m_Revit);
				this.m_TableInfos.Head = importExcelTableHeadForm.SelectTableHead;
			}
			this.Refresh();
		}

		private void InitData()
		{
			this.SetColMappint();
		}

		private void SetColMappint()
		{
			this.dbgPropertiesMapping.Rows.Clear();
			if (this.m_TableInfos.Head.IsNull())
			{
				return;
			}
			List<string> list = new List<string>();
			foreach (CombinationOfAttributes combinationOfAttributes in this.m_AllCombinationOfAttributesList)
			{
				if (combinationOfAttributes.AttributesName.Count <= 0)
				{
					string categoryName = combinationOfAttributes.CategoryName;
					list.Add(categoryName);
				}
			}
			List<TableHeadCell> effectiveRow = this.m_TableInfos.Head.GetEffectiveRow();
			int i = 0;
			int num = 1;
			while (i < effectiveRow.Count)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				string value = effectiveRow[i].Value;
				dataGridViewTextBoxCell.Value = value;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewComboBoxCell dataGridViewComboBoxCell = new DataGridViewComboBoxCell();
				foreach (string item in list)
				{
					dataGridViewComboBoxCell.Items.Add(item);
				}
				string text = "";
				if (effectiveRow[i].Key == null)
				{
					int num2 = this.FindDefaultMapping(value, list);
					if (num2 > -1)
					{
						text = dataGridViewComboBoxCell.Items[num2].ToString();
						dataGridViewComboBoxCell.Value = text;
						effectiveRow[i].Key = new TableHeadCellKey(text);
					}
				}
				else if (effectiveRow[i].Key.KeyName.CompareTo("") == 0)
				{
					int num3 = this.FindDefaultMapping(value, list);
					if (num3 > -1)
					{
						text = dataGridViewComboBoxCell.Items[num3].ToString();
						dataGridViewComboBoxCell.Value = text;
						effectiveRow[i].Key.KeyName = text;
					}
				}
				else
				{
					text = effectiveRow[i].Key.KeyName;
					dataGridViewComboBoxCell.Value = text;
				}
				dataGridViewRow.Cells.Add(dataGridViewComboBoxCell);
				DataGridViewDisableButtonCell dataGridViewDisableButtonCell = new DataGridViewDisableButtonCell();
				if (text.CompareTo("技术参数") == 0)
				{
					dataGridViewDisableButtonCell.Value = "...";
					dataGridViewDisableButtonCell.ToolTipText = "编辑“技术参数”";
					dataGridViewDisableButtonCell.FlatStyle = FlatStyle.Standard;
					dataGridViewDisableButtonCell.Enabled = true;
				}
				else
				{
					dataGridViewDisableButtonCell.Value = "";
					dataGridViewDisableButtonCell.ToolTipText = "";
					dataGridViewDisableButtonCell.FlatStyle = FlatStyle.Flat;
					dataGridViewDisableButtonCell.Enabled = false;
				}
				dataGridViewRow.Cells.Add(dataGridViewDisableButtonCell);
				this.dbgPropertiesMapping.Rows.Add(dataGridViewRow);
				num++;
				i++;
			}
		}

		private int FindDefaultMapping(string columnName, List<string> propertiesList)
		{
			string text = columnName.Replace(" ", "");
			string strB = "技术参数";
			int i = 0;
			int result = -1;
			while (i < propertiesList.Count)
			{
				string text2 = propertiesList[i];
				text2 = text2.Replace(" ", "");
				if (text.Contains(text2) || text2.Contains(text))
				{
					return i;
				}
				if (text2.CompareTo(strB) == 0)
				{
					result = i;
				}
				i++;
			}
			return result;
		}

		private void dbgPropertiesMapping_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			System.Windows.Forms.ComboBox comboBox = e.Control as System.Windows.Forms.ComboBox;
			if (comboBox != null)
			{
				comboBox.SelectedIndexChanged += this.DGVComboBoxSelectionChanged;
			}
		}

		private void DGVComboBoxSelectionChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dbgPropertiesMapping.CurrentCell.RowIndex;
			string text = ((System.Windows.Forms.ComboBox)sender).Text;
			if (text.CompareTo("技术参数") != 0)
			{
				this.m_TableInfos.Head.GetEffectiveRow()[rowIndex].Key = new TableHeadCellKey(text);
				DataGridViewDisableButtonCell dataGridViewDisableButtonCell = this.dbgPropertiesMapping.Rows[rowIndex].Cells[2] as DataGridViewDisableButtonCell;
				if (dataGridViewDisableButtonCell != null)
				{
					dataGridViewDisableButtonCell.Value = "";
					dataGridViewDisableButtonCell.ToolTipText = "";
					dataGridViewDisableButtonCell.FlatStyle = FlatStyle.Flat;
					dataGridViewDisableButtonCell.Enabled = false;
					return;
				}
			}
			else
			{
				DataGridViewDisableButtonCell dataGridViewDisableButtonCell2 = this.dbgPropertiesMapping.Rows[rowIndex].Cells[2] as DataGridViewDisableButtonCell;
				if (dataGridViewDisableButtonCell2 != null)
				{
					dataGridViewDisableButtonCell2.Value = "...";
					dataGridViewDisableButtonCell2.ToolTipText = "编辑“技术参数”";
					dataGridViewDisableButtonCell2.FlatStyle = FlatStyle.Standard;
					dataGridViewDisableButtonCell2.Enabled = true;
				}
			}
		}

		private void dbgPropertiesMapping_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 2)
			{
				if ((this.dbgPropertiesMapping.CurrentRow.Cells[1] as DataGridViewComboBoxCell).Value.ToString().CompareTo("技术参数") != 0)
				{
					return;
				}
				List<string> list = new List<string>();
				for (int i = 0; i < this.m_CategorySelectedListBox.CheckedItems.Count; i++)
				{
					string itemText = this.m_CategorySelectedListBox.GetItemText(this.m_CategorySelectedListBox.CheckedItems[i]);
					list.Add(itemText);
				}
				List<CombinationOfAttributes> list2 = new List<CombinationOfAttributes>();
				using (List<CombinationOfAttributes>.Enumerator enumerator = this.m_AllCombinationOfAttributesList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						CombinationOfAttributes combinationOfAttributes = enumerator.Current;
						if (list.FindIndex((string s) => s.CompareTo(combinationOfAttributes.CategoryName) == 0) >= 0)
						{
							list2.Add(combinationOfAttributes);
						}
					}
				}
				int rowIndex = this.dbgPropertiesMapping.CurrentCell.RowIndex;
				List<CombinationOfAttributes> combinationOfAttributes2 = this.m_TableInfos.Head.GetEffectiveRow()[rowIndex].Key.CombinationOfAttributes;
				string text = this.dbgPropertiesMapping.CurrentRow.Cells[0].Value.ToString();
				MaterialTechnicalParametersSettingForm materialTechnicalParametersSettingForm = new MaterialTechnicalParametersSettingForm(list2, combinationOfAttributes2, text);
				materialTechnicalParametersSettingForm.Text = "“" + text + "”" + materialTechnicalParametersSettingForm.Text;
				if (materialTechnicalParametersSettingForm.ShowDialog(this) != DialogResult.OK)
				{
					return;
				}
				this.m_TableInfos.Head.GetEffectiveRow()[rowIndex].Key = new TableHeadCellKey("技术参数");
				this.m_TableInfos.Head.GetEffectiveRow()[rowIndex].Key.CombinationOfAttributes = materialTechnicalParametersSettingForm.CurrentCombinationList;
			}
		}

		private ExternalCommandData m_Revit;

		private MaterialProgramInfos._Tag_TemplateType m_TemplateType;

		private string m_TemplatePath = "";

		private string m_ProfessionType = "";

		private TableInfos m_TableInfos;

		private List<CombinationOfAttributes> m_AllCombinationOfAttributesList = new List<CombinationOfAttributes>();

		private CheckedListBox m_CategorySelectedListBox;

		private SubjectType m_SubjectType;

		private Previewer m_Previewer;
	}
}
